package com.magima.utils;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class CorrectUtil {
	public static <T> List<T> getArrayModel(String result, Class<?> typeOfT) {
		try {
			JSONArray jsonArray = new JSONArray(result);
			Gson gson = new GsonBuilder().create();
			List<T> listData = new ArrayList<T>();
			for (int i = 0, length = jsonArray.length(); i < length; i++) {
				JSONObject jsonOb = (JSONObject) jsonArray.opt(i);
				@SuppressWarnings("unchecked")
				T o = (T) gson.fromJson(jsonOb.toString(), typeOfT);
				listData.add(o);
			}
			return listData;
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return null;
	}

	public static String correctOutputJsonString(String str) {
		return str.replace("\\", "\\\\").replace("\b", "\\b")
				.replace("\f", "\\f").replace("\r", "\\r").replace("\t", "\\t")
				.replace("\n", "\\n").replace("\"", "\\\"");
	}

	public static String removeSpecialCharactorString(String str) {
		return str.replace("\b", "").replace("\f", "").replace("\r", "")
				.replace("\t", "").replace("\n", "");
	}

	public static String escapeHtml(String str) {
		return str.replace("&", "&amp;").replace("<", "&lt;")
				.replace(">", "&gt;").replace(" ", "&ensp;");
	}
}
